package org.nowcoder.huawei.c200;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * 47：精准核酸检测
 */

public class HC047 {
    public static void main(String[] args) throws IOException {
        //输入处理
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        //人数
        int n = Integer.parseInt(br.readLine());

        //确诊人数
        List<Integer> badPeople = new ArrayList<>();
        for (String s : br.readLine().split(",")) {
            badPeople.add(Integer.parseInt(s));
        }

        //感染人群
        int[][] people = new int[n][n];
        for (int i = 0; i < n; i++) {
            String[] vals = br.readLine().split(",");
            for (int j = 0; j < vals.length; j++) {
                people[i][j] = "1".equals(vals[j]) ? 1 : 0;
            }
        }
        dfs(people, badPeople, n);
    }

    public static void dfs(int[][] people, List<Integer> badPeoples, int n) {
        boolean[] visited = new boolean[n];
        Stack<Integer> sta = new Stack<>();
        int count = 0;
        for (Integer p : badPeoples) {
            visited[p] = true;
            sta.add(p);
            while (!sta.isEmpty()) {
                int k = sta.pop();
                count++;
                for (int i = 0; i < n; i++) {
                    if (people[k][i] == 1 && !visited[i]) {
                        visited[i] = true;
                        sta.add(i);
                    }
                }
            }
        }
        System.out.println(count - badPeoples.size());
    }
}
